//
//  T3FOptions.swift
//  t3_map
//
//  Created by zhu on 2024/11/21.
//

import UIKit

/// 文本描述模型
struct T3FTextOption {

    enum T3FTextFont: String {
        case regular = "regular"
        case medium = "medium"
        case semiBold = "semiBold"
        case bold = "bold"
    }
    /// 文本样式描述模型
    struct T3FTextStyle {
        let color: String
        let fontSize: Double
        let fontWeight: T3FTextFont

        init(color: String, fontSize: Double, fontWeight: T3FTextFont) {
            self.color = color
            self.fontSize = fontSize
            self.fontWeight = fontWeight
        }

        init?(json: [String: Any]) {

            color = json["color"] as? String ?? "#1d2232"
            fontSize = json["fontSize"] as? Double ?? 12

            let fontWeightValue = json["fontWeight"] as? String ?? "regular"
            fontWeight = T3FTextFont(rawValue: fontWeightValue) ?? .regular
        }

        ///默认样式
        static var `default` = T3FTextStyle(color: "#1d2232", fontSize: 12, fontWeight: .regular)

        var uifont: UIFont? {
            switch fontWeight {
            case .regular:
                return UIFont(name: "PingFangSC-Regular", size: fontSize)
            case .medium:
                return UIFont(name: "PingFangSC-Medium", size: fontSize)
            case .semiBold:
                return UIFont(name: "PingFangSC-Semibold", size: fontSize)
            case .bold:
                return UIFont(name: "Helvetica-Bold", size: fontSize)
            }
        }

        var uiColor: UIColor {
            return T3FColorUtil.hex(color)
        }
    }

    let text: String
    let normalStyle: T3FTextStyle
    let highlightStyle: T3FTextStyle

    init?(json: [String: Any]) {
        guard let text = json["text"] as? String else { return nil }
        self.text = text

        let normal: T3FTextStyle
        if let normalStyle = json["normalStyle"] as? [String : Any] {
            normal = T3FTextStyle(json: normalStyle) ?? .default
        }
        else {
            normal = .default
        }

        self.normalStyle = normal

        let highlight: T3FTextStyle
        if let highlightStyle = json["highlightStyle"] as? [String : Any] {
            highlight = T3FTextStyle(json: highlightStyle) ?? .default
        }
        else {
            highlight = normal
        }

        self.highlightStyle = highlight
    }
}

struct T3FImageOption {
    let imageName: String
    let width: Double
    let height: Double
    let rotation: Double

    init?(json: [String: Any]) {
        guard let name = json["imageName"] as? String else { return nil }
        self.imageName = name

        let width = json["width"] as? Double ?? 28
        let height = json["height"] as? Double ?? 28
        let rotation = json["rotation"] as? Double ?? 0
        self.width = width
        self.height = height
        self.rotation = rotation
    }
}

class T3FBubbleOption {
    private let mainKey = "content"

    let texts: [String: T3FTextOption]
    let contentText: T3FTextOption
    let bubbleType: T3BubbleType
    let showArrow: Bool

    init?(json: [String: Any]) {
        guard let bubbleTypeString = json["bubbleType"] as? String,
                let bubbleType = T3BubbleType(rawValue: bubbleTypeString) else {
            return nil
        }
        guard let showArrow = json["showArrow"] as? Bool else {
            return nil
        }
        guard let texts = json["texts"] as? [String: [String:Any]], !texts.isEmpty else {
            return nil
        }
        var contents = [String: T3FTextOption]()
        texts.forEach { (key: String, value: [String : Any]) in
            if let model = T3FTextOption(json: value) {
                contents[key] = model
            }
        }
        guard !contents.isEmpty, let mainText = contents[mainKey] else {
            return nil
        }
        self.contentText = mainText
        self.bubbleType = bubbleType
        self.showArrow = showArrow
        self.texts = contents
    }
}

/// valuation
extension T3FBubbleOption {
    var timeTextOption: T3FTextOption? {
        let key = "time"
        return texts[key]
    }

    var timeTipsTextOption: T3FTextOption? {
        let key = "timeTips"
        return texts[key]
    }


    var stopInfoTextOption: T3FTextOption? {
        let key = "stopInfo"
        return texts[key]
    }
}
